package Persistance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import Model.Mineral;

public class MineralDataMapper implements IDataMapper<Mineral>
{

	public boolean insert(Mineral mineral) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try 
		{
			PreparedStatement s = conn.prepareStatement("insert into mineral values (?,?,?)");
			s.setString(1, mineral.getCodigo());
			s.setString(2, mineral.getNombre());
			s.setString(3, AdministradorPersistencia.booleanToString(mineral.isActivo()));
			
			int rowCount = s.executeUpdate();
			return rowCount == 1;
		} 
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
	}

	public boolean update(Mineral mineral) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try 
		{
			PreparedStatement s = conn.prepareStatement("update mineral "
							+ "set nombre = ? "
							+ "where codigo = ?" );
			s.setString(1, mineral.getNombre());
			s.setString(2, mineral.getCodigo());

			int rowCount = s.executeUpdate();
			return rowCount == 1;
		} 
		catch (Exception e) 
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
	}

	public boolean delete(Mineral mineral) 
	{
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try
		{
			PreparedStatement s = conn.prepareStatement("update mineral set activo = 'F' where codigo = ?");
			s.setString(1, mineral.getCodigo());
			
			int rowCount = s.executeUpdate();
			return rowCount == 1;
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
			return false;
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
		
	}

	public List<Mineral> findAll() 
	{
		List <Mineral> minerales = new ArrayList<Mineral>();
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try
		{
			Statement s = conn.createStatement();
			ResultSet result = s.executeQuery("select * from mineral");
			
			while (result.next())
			{
				String codigo = result.getString(1);
				String nom = result.getString(2);
				boolean activo = AdministradorPersistencia.stringToBoolean(result.getString(3));
				
				Mineral m = new Mineral(codigo, nom, activo);
				minerales.add(m);
			}
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
		
		return minerales;
	}

	public Mineral findById(Mineral mineral) 
	{
		Mineral m = null;
		Connection conn = PoolConnection.getPoolConnection().getConnection();
		
		try
		{	
			PreparedStatement s = conn.prepareStatement("select * from mineral where codigo = ?");
			s.setString(1, mineral.getCodigo());
			ResultSet result = s.executeQuery();
			
			if(result.next())
			{
				String cod = result.getString(1);
				String nom = result.getString(2);
				boolean activo = AdministradorPersistencia.stringToBoolean(result.getString(3));
				
				m = new Mineral(cod, nom, activo);
			}
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		finally
		{
			PoolConnection.getPoolConnection().realeaseConnection(conn);
		}
		
		return m;
	}

}
